package warmachineroster.service.validation.calculators;

import java.util.List;

import warmachineroster.Roster;
import warmachineroster.service.validation.UnitCounter;
import warmachinesystem.BaseUnit;

public class PointValueCalculator {
	
	private Roster roster;
	
	public PointValueCalculator(Roster roster) {
		this.roster = roster;
	}
	
	protected Roster getRoster() {
		return roster;
	}

	public int calculatePointValue(TierStatus status, UnitCounter counter) {
		int pointValue = getRoster().getPointValue();
		List<BaseUnit> baseUnits = counter.getUnits();
		for (BaseUnit bu : baseUnits) {
			Integer pointCostDelta = status.getUnitCostDelta().get(bu);
			if (pointCostDelta != null) {
				pointValue += pointCostDelta;
			} 
			Integer freeUnitNumber = status.getFreeUnits().get(bu);
			if (freeUnitNumber != null){
				Integer count = counter.getCount(bu);
				if (count > 0 && count >= freeUnitNumber){
					pointValue -= bu.getPointCost() * freeUnitNumber;
				} else if (count > 0 && count < freeUnitNumber){
					pointValue -= bu.getPointCost() * count;
				}
			}
		}

		return pointValue;
	}



}
